package com.cwj.proxy;

import net.sf.cglib.proxy.Enhancer;
import net.sf.cglib.proxy.MethodInterceptor;
import net.sf.cglib.proxy.MethodProxy;

import java.lang.reflect.Method;

/**
 * @author chenwujie
 * @date 2020-12-30 15:37
 */
public class HelloWorldProxy implements MethodInterceptor {
    @Override
    public Object intercept(Object o, Method method, Object[] objects, MethodProxy methodProxy) throws Throwable {
        System.out.println("前");
        Object result = methodProxy.invokeSuper(o, objects);
        System.out.println("目标方法返回结果：" + result);
        System.out.println("后");
        return result;
    }

    public static void main(String[] args) {
        Enhancer enhancer = new Enhancer();
        enhancer.setSuperclass(HelloWorld.class);
        enhancer.setCallback(new HelloWorldProxy());
        HelloWorld helloWorld = (HelloWorld) enhancer.create();
        System.out.println("代理对象调用方法 开始：");
        String hello = helloWorld.hello();
        System.out.println("代理对象返回结果：" + hello);
    }
}
